<script setup lang="ts">
import {markRaw, onMounted, ref} from "vue";
import type {EChartsOption, EChartsType} from "echarts";
import * as echarts from "echarts";
import {useGetTreeDataApi} from "@/api";
import type {Tree} from "@/interface";
import 'echarts-wordcloud'

const tree = ref()
const option = ref<EChartsOption>()
const myChart = ref<EChartsType>()
const treeData = ref<Tree>({
  data:[]
})
const init = async () => {
  const {data:res} = await useGetTreeDataApi()
  treeData.value = res
  myChart.value = markRaw(echarts.init(tree.value));
  draw();
}
const draw = () => {
  option.value = {
    title: {
      text: '全部新能源车型',
      left: 'center'
    },
    tooltip: {},
    series: [
      {
        name: '全部',
        type: 'treemap',
        visibleMin: 300,
        label: {
          show: true,
          formatter: '{b}'
        },
        upperLabel: {
          show: true,
          height: 30
        },
        itemStyle: {
          borderColor: '#fff'
        },
        levels: [
          {
            itemStyle: {
              borderColor: '#777',
              borderWidth: 0,
              gapWidth: 1
            },
            upperLabel: {
              show: false
            }
          },
          {
            itemStyle: {
              borderColor: '#555',
              borderWidth: 5,
              gapWidth: 1
            },
            emphasis: {
              itemStyle: {
                borderColor: '#ddd'
              }
            }
          },
          {
            colorSaturation: [0.35, 0.5],
            itemStyle: {
              borderWidth: 5,
              gapWidth: 1,
              borderColorSaturation: 0.6
            }
          }
        ],
        data: treeData.value.data
      }
    ]
  }
  myChart.value.setOption(option.value, {notMerge: true});
}
onMounted(() => {
  init()
})
</script>

<template>
  <div ref="tree" class="tree">
  </div>
</template>

<style scoped>
.tree{
  width: 100%;
  height: 100%;
}
</style>